package com.lvmama.common.utils;

import java.util.List;

import com.github.pagehelper.PageInfo;

public class PageInfoUtil <T> extends PageInfo<T>{
   

    /**
	 * 
	 */
	private static final long serialVersionUID = -3898736478083883465L;

	public PageInfoUtil() {
    	super();
    }

    public PageInfoUtil(List<T> list,  int pageNum, int pageSize, long total, int startRow, int endRow){
    	super();
        super.setPageNum(pageNum);
        super.setPageSize(pageSize);
        super.setTotal(total);
        super.setPages(setTotalPages());
        super.setList(list);
        super.setSize(list.size());
        super.setNavigatePages(7);
        super.setStartRow(startRow + 1);
        super.setEndRow(endRow + 1);
        //计算导航页
        calcNavigatepageNums();
        //计算前后页，第一页，最后一页
        calcPage();
        //判断页面边界

        judgePageBoudary();
    }

    public int setTotalPages(){
        int p = (int) (super.getTotal() / super.getPageSize());
        if (super.getTotal() % super.getPageSize() == 0)
            return p;
        else
            return p + 1;
    }

    /**
     * 计算导航页
     */
    private void calcNavigatepageNums() {
        //当总页数小于或等于导航页码数时
        if (super.getPages() <= super.getNavigatePages()) {
            super.setNavigatepageNums( new int[super.getPages()]);
            for (int i = 0; i < super.getPages(); i++) {
            	 super.getNavigatepageNums()[i] = i + 1;
            }
        } else { //当总页数大于导航页码数时
        	super.setNavigatepageNums( new int[super.getNavigatePages()]);
            int startNum = super.getPageNum() - super.getNavigatePages() / 2;
            int endNum = super.getPageNum() + super.getNavigatePages() / 2;

            if (startNum < 1) {
                startNum = 1;
                //(最前navigatePages页
                for (int i = 0; i < super.getNavigatePages(); i++) {
                	 super.getNavigatepageNums()[i] = startNum++;
                }
            } else if (endNum > super.getPages()) {
                endNum = super.getPages();
                //最后navigatePages页
                for (int i = super.getNavigatePages() - 1; i >= 0; i--) {
                	 super.getNavigatepageNums()[i] = endNum--;
                }
            } else {
                //所有中间页
                for (int i = 0; i < super.getNavigatePages(); i++) {
                	 super.getNavigatepageNums()[i] = startNum++;
                }
            }
        }
    }

    /**
     * 计算前后页，第一页，最后一页
     */
    private void calcPage() {
        if (super.getNavigatepageNums() != null && super.getNavigatepageNums().length > 0) {
            super.setFirstPage(super.getNavigatepageNums()[0]);
            super.setLastPage(super.getNavigatepageNums()[super.getNavigatepageNums().length -1]);
            if (super.getPageNum() > 1) {
            	super.setPrePage(super.getPageNum() - 1);
            }
            if (super.getPageNum() < super.getPages()) {
            	super.setNextPage(super.getPageNum() + 1);
            }
        }
    }

    /**
     * 判定页面边界
     */
    private void judgePageBoudary() {
    	super.setIsFirstPage(super.getPageNum() == 1);
    	super.setIsLastPage(super.getPageNum() == super.getPages());
    	super.setHasPreviousPage(super.getPageNum() > 1);
    	super.setHasNextPage(super.getPageNum() < super.getPages());
    }
   
}
